/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utn.tsb.tej.world_logic;

import java.io.Serializable;
import csm.uttils.generics.Roster;

/**
 *
 * @author Administrador
 */
public class Country extends Card implements Serializable {

    private Color color;
    private int armies;
    private int missile;
    private int xPosition;
    private int yPosition;
    private Roster<Country> landBorderingCountries;
    private Roster<Country> seaBorderingCountries;

    public Country(String name, CardType type, Color color, int xPosition, int yPosition) {
        super(name, type);
        this.color = color;
        this.xPosition = xPosition;
        this.yPosition = yPosition;
        landBorderingCountries = new Roster();
        landBorderingCountries = new Roster();
        armies = 0;
        missile = 0;
    }

    public int getxPosition() {
        return xPosition;
    }

    public void setxPosition(int xPosition) {
        this.xPosition = xPosition;
    }

    public int getyPosition() {
        return yPosition;
    }

    public void setyPosition(int yPosition) {
        this.yPosition = yPosition;
    }

    public Country(String name, CardType type, Color color, int armies, int missile, Roster landBorderingCountries, Roster seaBorderingCountries) {
        super(name, type);
        this.color = color;
        this.armies = armies;
        this.missile = missile;
        this.landBorderingCountries = landBorderingCountries;
        this.seaBorderingCountries = seaBorderingCountries;
    }

    public Country() {
        landBorderingCountries = new Roster<Country>();
        seaBorderingCountries = new Roster<Country>();
        armies = 0;
        missile = 0;
    }

    public int getArmies() {
        return armies;
    }

    public void setArmies(int armies) {
        this.armies = armies;
    }

    public boolean addArmie(int amnt) {
        boolean out = false;
        if (!isBlocked()) {
            armies += amnt;
            out = true;
        }
        return out;
    }

    public void subArmy(int amnt) {
        armies -= amnt;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Roster<Country> getLandBorderingCountries() {
        return landBorderingCountries;
    }

    public void setLandBorderingCountries(Roster<Country> landBorderingCountries) {
        this.landBorderingCountries = landBorderingCountries;
    }

    public int getMissile() {
        return missile;
    }

    public void setMissile(int missile) {
        this.missile = missile;
    }

    public Roster<Country> getSeaBorderingCountries() {
        return seaBorderingCountries;
    }

    public void setSeaBorderingCountries(Roster<Country> seaBorderingCountries) {
        this.seaBorderingCountries = seaBorderingCountries;
    }

    /**
     * returns the player that owns the country in the last round on the World
     * Class
     * @return
     */
    public Player getOwner() {
        Player player;
        Roster<Player> players = WorldLogicFacade.world.getRounds().getLast().getTurns();
        for (int i = 0; i < players.count(); i++) {
            player = players.getElementInPosition(i);
            if (player.isOwnerOfCountry(this)) {
                return player;
            }
        }
        System.out.println("pis sin dueño!?!?");
        return null;
    }

    public Continent getContainerContinent() {
        Continent continent = null;
        Roster<Continent> continents = WorldLogicFacade.world.getContinents();
        for (int i = 0; i < continents.count(); i++) {
            if (continents.getElementInPosition(i).containsCountry(this)) {
                continent = continents.getElementInPosition(i);
            }
        }
        return continent;
    }

    public boolean isLandBordering(Country coutry) {
        boolean is = false;
        if (landBorderingCountries.count() != 0) {
            if (landBorderingCountries.indexOf(coutry) != -1) {
                is = true;
            }
        }
        return is;
    }

    public boolean isSeaBordering(Country coutry) {
        boolean is = false;
        if (seaBorderingCountries.count() != 0) {
            if (seaBorderingCountries.indexOf(coutry) != -1) {
                is = true;
            }
        }
        return is;
    }

    public Roster<Country> getAttackableCountries() {
        Roster<Country> attackable = new Roster<Country>();
        for (int i = 0; i < this.landBorderingCountries.count(); i++) {
            attackable.addFirst(this.landBorderingCountries.getElementInPosition(i));
        }
        for (int i = 0; i < this.seaBorderingCountries.count(); i++) {
            attackable.addFirst(this.seaBorderingCountries.getElementInPosition(i));
        }
        Situation situation = WorldLogicFacade.world.getRounds().getLast().getSituation();
        if (situation instanceof SituationBorder) {
            SituationBorder sb = (SituationBorder) situation;
            Roster<Country> cc = this.getContainerContinent().getCountries();
            if (sb.isOpen()) {
                for (int i = 0; i < cc.count(); i++) {
                    if(attackable.isEmpty()) return attackable;
                    attackable.remove(cc.getElementInPosition(i));
                }
            } else {
                Roster<Country> aux = new Roster<Country>();
                for (int i = 0; i < cc.count(); i++) {
                    if(attackable.isEmpty()) return attackable;
                    Country country = attackable.remove(cc.getElementInPosition(i));
                    if (country != null) {
                        aux.addFirst(country);
                    }
                }
                attackable.clear();
                attackable = aux;
            }
        }
        Roster<Country> paisesDelMismoJugador = this.getOwner().getCountries();
        for (int i = 0; i < paisesDelMismoJugador.count(); i++) {
            if(attackable.isEmpty()) return attackable;
            attackable.remove(paisesDelMismoJugador.getElementInPosition(i));
        }
        return attackable;
    }

    public boolean isBlocked() {
        boolean blocked = true;
        Roster<Country> neighbors = new Roster<Country>();
        for (int i = 0; i < seaBorderingCountries.count(); i++) {
            neighbors.addInOrder(seaBorderingCountries.getElementInPosition(i));
        }
        for (int i = 0; i < landBorderingCountries.count(); i++) {
            neighbors.addInOrder(landBorderingCountries.getElementInPosition(i));
        }
        Player blocker = neighbors.getFirst().getOwner();
        for (int i = 0; i < neighbors.count(); i++) {
            if (neighbors.getElementInPosition(i).getOwner() != blocker || neighbors.getElementInPosition(i).getArmies() < 2) {
                blocked = false;
            }
        }
        return blocked;
    }

    @Override
    public String toString() {
        String out = "";
        out += name + "(" + armies + ")";
        return out;
    }
}
